//
//  GQSongSheetController.m
//  MyMusic
//
//  Created by sunny on 16/4/3.
//  Copyright © 2016年 sunny. All rights reserved.
//

#import "GQSongSheetController.h"
#import "GQSongSheetCell.h"
#import <AFHTTPSessionManager.h>
#import <MJExtension.h>
#import "GQSongSheetItem.h"
#import <MJRefresh.h>
#import "GQGeDanInfoViewController.h"
static NSString * const reuseIdentifier = @"SongSheetCell";
static NSString * const SongSheetHeaderCell = @"SongSheetHeaderCell";

@interface GQSongSheetController ()
@property (nonatomic,strong)NSArray *itemArray;
@end

@implementation GQSongSheetController
- (NSArray *)itemArray
{
    if (!_itemArray) {
        _itemArray = [NSArray array];
    }
    return _itemArray;
}

+ (instancetype)SongSheetController
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.sectionInset = UIEdgeInsetsMake(10, 15, 0, 15);
    layout.itemSize = CGSizeMake(160, 200);
    layout.minimumLineSpacing = 20;
//    设置组头部视图的size
    layout.headerReferenceSize = CGSizeMake(GQUIScreenW, 35);
    
    GQSongSheetController *vc = [[GQSongSheetController alloc] initWithCollectionViewLayout:layout];
    
    return vc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, GQGlobleTabBarHeight, 0);
    
    // Register cell classes
    [self.collectionView registerNib:[UINib nibWithNibName:@"GQSongSheetCell" bundle:nil] forCellWithReuseIdentifier:reuseIdentifier];
    
//    注册组头部视图
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:SongSheetHeaderCell];
    
    self.collectionView.mj_header = [GQDIYRefreshHeader gq_headerWithRefreshingBlock:^{
        [self loadData];
    }];
    
//    开始刷新
    [self.collectionView.mj_header beginRefreshing];
 
//    设置尾部视图
   self.collectionView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
       [self loadMoreData];
   }];
}

//加载最新数据
- (void)loadData
{
    [GQHttpRequest GET:@"http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.7.2.0&channel=xiaomi&operator=2&method=baidu.ting.diy.gedan&page_size=30&page_no=1" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        self.itemArray = [GQSongSheetItem mj_objectArrayWithKeyValuesArray:responseObject[@"content"]];
        
//        刷新数据
        [self.collectionView reloadData];
        
//        结束刷新
        [self.collectionView.mj_header endRefreshing];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        //        结束刷新
        [self.collectionView.mj_header endRefreshing];
    }];
}

//加载更多数据
- (void)loadMoreData
{
    [GQHttpRequest GET:@"http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.7.2.0&channel=xiaomi&operator=2&method=baidu.ting.diy.gedan&page_size=30&page_no=2" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.itemArray = [GQSongSheetItem mj_objectArrayWithKeyValuesArray:responseObject[@"content"]];
        
        //        刷新数据
        [self.collectionView reloadData];
        
        //        结束刷新
        [self.collectionView.mj_footer endRefreshing];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        //        结束刷新
        [self.collectionView.mj_footer endRefreshing];
    }];
}

#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.itemArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GQSongSheetCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    GQSongSheetItem *item = self.itemArray[indexPath.row];
    cell.songSheetItem = item;
    
    return cell;
}

//设置组头部视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:SongSheetHeaderCell forIndexPath:indexPath];
    
    UILabel *label = [[UILabel alloc] initWithFrame:header.bounds];
    label.gq_x = 10;
    label.text = @"全部歌单";
    [header addSubview:label];
    return header;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    GQSongSheetCell *cell = (GQSongSheetCell *)[collectionView cellForItemAtIndexPath:indexPath];
    GQGeDanInfoViewController *geDanVc = [[GQGeDanInfoViewController alloc]init];
    geDanVc.listid = cell.songSheetItem.listid;
    geDanVc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:geDanVc animated:YES];
    
}
@end
